import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore('user', () => {
  const userName = ref('Your Name')
  const userTitle = ref('Web Developer')
  const userBio = ref('Passionate about building modern web applications')
  const userAvatar = ref('')
  
  // 从localStorage初始化
  if (localStorage.getItem('user')) {
    const savedUser = JSON.parse(localStorage.getItem('user')!)
    userName.value = savedUser.name || userName.value
    userTitle.value = savedUser.title || userTitle.value
    userBio.value = savedUser.bio || userBio.value
    userAvatar.value = savedUser.avatar || userAvatar.value
  }
  
  // 更新用户信息
  const updateUser = (data: any) => {
    if (data.name) userName.value = data.name
    if (data.title) userTitle.value = data.title
    if (data.bio) userBio.value = data.bio
    if (data.avatar) userAvatar.value = data.avatar
    
    // 保存到localStorage
    localStorage.setItem('user', JSON.stringify({
      name: userName.value,
      title: userTitle.value,
      bio: userBio.value,
      avatar: userAvatar.value
    }))
  }
  
  return { 
    userName, 
    userTitle, 
    userBio, 
    userAvatar,
    updateUser
  }
})